package com.bookquery.servlet;

import com.bookquery.entity.BookQuery;
import com.bookquery.service.BookQueryService;
import com.bookquery.service.impl.BookQueryServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(urlPatterns = {"/detail"}, name = "ShowDetailServlet")
public class ShowDetailServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        BookQueryService bookQueryService = new BookQueryServiceImpl();
        PrintWriter out = response.getWriter();
        String bookId = request.getParameter("bookId");
        int id =0;
        if (bookId != null) {
            id = Integer.parseInt(bookId);
        }
        BookQuery book = bookQueryService.queryBookById(id);
        request.getSession().setAttribute("book", book);
        request.getRequestDispatcher("/detail.jsp").forward(request,response);
    }
}
